package com.bytedance.article.common.nativecrash;

import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

public class JniToJava {
    public JniToJava() {

    }

    private static void a(File arg3, String arg4) throws IOException {
        FileOutputStream v1 = null;
        if (!TextUtils.isEmpty(arg4)) {
            try {
                v1 = new FileOutputStream(arg3, true);
                ((OutputStream) v1).write((arg4 + "\n").getBytes());
            } catch (Throwable v0) {
            } finally {
                if (v1 != null) {
                    try {
                        v1.close();
                    } catch (Exception e) {

                    }
                }
            }
        }
    }

    public static void getJavaStack(String arg7, String arg8) {
        File v0_1;
        int v3;
        StringBuilder v2;
        StackTraceElement[] v0;
        int v1 = 0;
        if (TextUtils.isEmpty(((CharSequence) arg7))) {
            return;
        }

        if (TextUtils.isEmpty(((CharSequence) arg8))) {
            return;
        }

        if ("main".equalsIgnoreCase(arg7)) {
            v0 = Looper.getMainLooper().getThread().getStackTrace();
            v2 = new StringBuilder();
            v2.append("below is native thread corresponding java thread stack:\n");
            v3 = v0.length;
            while (v1 < v3) {
                v2.append("at  " + v0[v1].toString() + "\n");
                ++v1;
            }

            if (TextUtils.isEmpty(((CharSequence) arg8))) {
                return;
            }

            v0_1 = new File(arg8);
            try {
                JniToJava.a(v0_1, v2.toString());
            } catch (IOException v0_2) {
                v0_2.printStackTrace();
            }

            Log.e("womendejiamain", v2.toString());
            return;
        }

        Set v0_3 = Thread.getAllStackTraces().keySet();
        Object[] v0_4 = v0_3.toArray(new Thread[v0_3.size()]);
        v3 = v0_4.length;
        int v2_1;
        for (v2_1 = 0; v2_1 < v3; ++v2_1) {
            Object v4 = v0_4[v2_1];
            if (((Thread) v4).getName().equals(arg7)) {
                v0 = ((Thread) v4).getStackTrace();
                v2 = new StringBuilder();
                v2.append("below is native thread corresponding java thread stack:\n");
                v3 = v0.length;
                while (v1 < v3) {
                    v2.append("at  " + v0[v1].toString() + "\n");
                    ++v1;
                }

                if (!TextUtils.isEmpty(((CharSequence) arg8))) {
                    v0_1 = new File(arg8);
                    try {
                        JniToJava.a(v0_1, v2.toString());
                    } catch (IOException v0_2) {
                        v0_2.printStackTrace();
                    }

                    Log.e("womendejia", v2.toString());
                }
                return;
            }
        }

        StackTraceElement[] v2_2 = Looper.getMainLooper().getThread().getStackTrace();
        StringBuilder v3_1 = new StringBuilder();
        v3_1.append("native thread has no java thread below is the main thread stack when native crash:\n");
        int v4_1 = v2_2.length;
        int v0_5;
        for (v0_5 = 0; v0_5 < v4_1; ++v0_5) {
            v3_1.append("at  " + v2_2[v0_5].toString() + "\n");
        }

        if (TextUtils.isEmpty(arg8)) {
            return;
        }

        v0_1 = new File(arg8);
        try {
            JniToJava.a(v0_1, v3_1.toString());
        } catch (IOException v0_2) {
            v0_2.printStackTrace();
        }

        Log.e("womendejiamain", v3_1.toString());
        return;
    }
}

